#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <cstring>

using namespace std;


void *threadRun(void* args)
{
    pthread_detach(pthread_self());//pthread_self返回该线程的id
    int count = 3;
    while(count--)
    {
        cout << "子线程: " << getpid() <<endl;
        sleep(1);
    }
    return nullptr;
}

int main()
{
    pthread_t tid;
    pthread_create(&tid, nullptr, threadRun, nullptr);

    sleep(1);//要保证pthread_detach在pthread_join之前触发
    int n = pthread_join(tid, nullptr);
    printf("n = %d, who = 0x%x, why: %s\n", n, tid, strerror(n));
}